package com.example.truyencoandersen.fragments;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.truyencoandersen.FragmentChangeActivity;
import com.example.truyencoandersen.R;
import com.example.truyencoandersen.customview.CurlPage;
import com.example.truyencoandersen.customview.CurlView;

public class TruyenFragment extends Fragment {

	private int truyen_index = -1;

	private CurlView mCurlView;
	
	private String[] contents;

	public TruyenFragment() {
		this(0);
	}

	public TruyenFragment(int index) {
		truyen_index = index;
		setRetainInstance(true);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (savedInstanceState != null)
			truyen_index = savedInstanceState.getInt("index");

		switch(truyen_index) {
			case 0:
				contents = getResources().getStringArray(R.array.doi_giay_hanh_phuc);
				break;
			case 5:
				contents = getResources().getStringArray(R.array.nang_cong_chua_va_hat_dau);
				break;
			case 9:
				contents = getResources().getStringArray(R.array.cay_lua_mach);
				break;
			case 15:
				contents = getResources().getStringArray(R.array.doi_giay_batket_tu_di_duoc);
				break;
			default:
				contents = getResources().getStringArray(R.array.doi_giay_hanh_phuc);
				break;
		}
		
		String title = getResources().getStringArray(R.array.menu_truyen)[truyen_index];

		// Set title bar
		((FragmentChangeActivity) getActivity()).setTitle(title);

		View v = inflater.inflate(R.layout.truyen, container, false);

		int index = 0;

		mCurlView = (CurlView) v.findViewById(R.id.curl);
		mCurlView.setKeepScreenOn(true);
		mCurlView.setPageProvider(new PageProvider());
		mCurlView.setSizeChangedObserver(new SizeChangedObserver());
		mCurlView.setCurrentIndex(index);
		mCurlView.setBackgroundColor(0xFF202830);

		// This is something somewhat experimental. Before uncommenting next
		// line, please see method comments in CurlView.
		mCurlView.setEnableTouchPressure(true);

		return v;
	}

	@Override
	public void onPause() {
		super.onPause();
		mCurlView.onPause();
	}

	@Override
	public void onResume() {
		super.onResume();
		mCurlView.onResume();
	}

	private class PageProvider implements CurlView.PageProvider {

		public PageProvider() {
		}

		@Override
		public int getPageCount() {
			return contents.length;
		}

		@SuppressLint("NewApi") private Bitmap loadBitmap(int width, int height, int index) {
			Bitmap txtBitmap = Bitmap.createBitmap(width, height,
					Bitmap.Config.ARGB_8888);
			String content = contents[index];
			Canvas c = new Canvas(txtBitmap);
			View mView = getActivity().getLayoutInflater().inflate(R.layout.text_truyen, null, false);
			TextView tv = (TextView) mView.findViewById(R.id.text_story);
			
			Drawable bg = getResources().getDrawable( R.drawable.bg_story);
			tv.setBackground(bg);
			
			tv.setText(content);
			tv.setTextColor(Color.BLACK);
			//tv.setTextSize(19);
			tv.setLinksClickable(true);
			tv.setLineSpacing(2f, 1.5f);
			Typeface mFont = Typeface.createFromAsset(getActivity().getAssets(), "font2.ttf");
			tv.setTypeface(mFont,Typeface.BOLD);
			tv.layout(10, 0, getResources().getDisplayMetrics().widthPixels,
					getResources().getDisplayMetrics().heightPixels);
			tv.draw(c);
			
			c.drawBitmap(txtBitmap, 0, 0, null);

			return txtBitmap;
		}

		@Override
		public void updatePage(CurlPage page, int width, int height, int index) {

			switch (index) {
			case 0: {
				Bitmap front = loadBitmap(width, height, 0);
				page.setTexture(front, CurlPage.SIDE_FRONT);
				page.setColor(Color.rgb(180, 180, 180), CurlPage.SIDE_BACK);
				break;
			}
//			case 4: {
//				Bitmap front = loadBitmap(width, height, 0);
//				page.setTexture(front, CurlPage.SIDE_BOTH);
//				page.setColor(Color.argb(127, 255, 255, 255),
//						CurlPage.SIDE_BACK);
//				break;
//			}
			default: {
				Bitmap front = loadBitmap(width, height, index);
				Bitmap back = loadBitmap(width, height, index);
				page.setTexture(front, CurlPage.SIDE_FRONT);
				page.setTexture(back, CurlPage.SIDE_BACK);
				break;
			}
			}
		}

	}

	/**
	 * CurlView size changed observer.
	 */
	private class SizeChangedObserver implements CurlView.SizeChangedObserver {
		@Override
		public void onSizeChanged(int w, int h) {
			if (w > h) {
				mCurlView.setViewMode(CurlView.SHOW_TWO_PAGES);
				mCurlView.setMargins(.1f, .05f, .1f, .05f);
			} else {
				mCurlView.setViewMode(CurlView.SHOW_ONE_PAGE);
				mCurlView.setMargins(.1f, .1f, .1f, .1f);
			}
		}
	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("index", truyen_index);
	}

}
